package org.projectwithoutname.ui.wizard.pages;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.projectwithoutname.ui.wizard.pages.composite.InfoSeasonComposite;
import org.projectwithoutname.ui.wizard.pages.composite.NameImageComposite;

public class InfoSeasonPage extends WizardPage 
{
	private InfoSeasonComposite container;

	public InfoSeasonPage(String pageName) {
		super(pageName);
		setTitle(pageName);
		setDescription("Insert the general information about this season");
		setPageComplete(false);
	}

	@Override
	public void createControl(Composite parent) 
	{
		
		container = new InfoSeasonComposite(parent,SWT.None);
		
		container.getTextName().addKeyListener(new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent e){
			}
			@Override
			public void keyReleased(KeyEvent e){
				validatePage();
			}
		});
		
		container.getDescription().addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				validatePage();
			}
		});
		
		container.getComments().addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				validatePage();
			}
		});
		
		container.getDatePrimier().addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				validatePage();
			}
		});
		
		container.getDateEnd().addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				validatePage();
			}
		});

		setControl(container);

	}
	
	private  void validatePage(){
		setPageComplete(true);
		String name = container.getTextName().getText();
		String description = container.getDescription().getText();
		DateTime begin = container.getDatePrimier();
		DateTime end = container.getDateEnd();
		
		if(name.isEmpty() || name.equals("")){
			setPageComplete(false);
			setErrorMessage("The serie's name cannot be empty");
			return;
		}
		else if((name.charAt(0) >= 'a') || (name.charAt(0) >= 'z')){
			setPageComplete(false);
			setErrorMessage("The serie's name cannot starts with lower case");
		}
		else if((name.charAt(0) == ' ') || (name.charAt(name.length()-1) == ' ')){
			setPageComplete(false);
			setErrorMessage("The serie's name cannot starts/ends with space");
		}
		else if (description.equals("")){
			setPageComplete(false);
			setErrorMessage("Any description must to be set");
		}
		else if (begin.getYear() > end.getYear()){
			setPageComplete(false);
			setErrorMessage("Season premier date cannot be older than season ends date");
		}
		else if (begin.getYear() == end.getYear()){
			if (begin.getMonth() > end.getMonth()){
				setPageComplete(false);
				setErrorMessage("Season premier date cannot be older than season ends date");
			}
			else if (begin.getDay() > end.getDay()){
				setPageComplete(false);
				setErrorMessage("Season premier date cannot be older than season ends date");
			} else{
				setErrorMessage(null);
				return;
			}
		}
		else{
			setDescription("Click on Finish");
			setErrorMessage(null);
			return;
		}
			
	}
	
//		if(!ValidateString.validateString(name))
//		{
//			setPageComplete(false);
//			if(ValidateString.character == ' ')
//				setErrorMessage("Please remove the whitespace from the end of field");
//			else
//				setErrorMessage(ValidateString.character+" is an invalid character in resource: "+name);
//			return;
//		}
//		else
//		{
//			setDescription("Click on Finish");
//			setErrorMessage(null);
//			setPageComplete(true);
//		}		
//	}
	
	public String getName(){
		return container.getTextName().getText();
	}
	
	public String getDescription(){
		return container.getDescription().getText();
	}
	

	public String getComments(){
		return container.getComments().getText();
	}
	
	public DateTime getDatePremier(){
		return container.getDatePrimier();
	}
	
	public DateTime getDateEnd(){
		return container.getDateEnd();
	}

}
